import { Util } from '../../utils/util.js';
import { CsDouble } from '../common/csDouble.js';
import { Definition } from '../common/definition.js';
import { OrderInventoryHandleMethod } from '../common/docDefinition.js';
import { Engine } from '../engine.js';
//import { FunctionPermissionDefinition } from '@/platform/function_permission_definition.js';

export class OrgSettings {
    constructor(taxRate = 13) {
        this.testSwitch = false;
        this.borderTest = false;
        this.logFileTest = false;
        this.easyMode = true;
        this.autoFillOrderWithDefaultWarehouse = true;
        this.autoFillOrderWithDefaultAccount = true;
        this.autoFillOrderWithDefaultCustomer = false;
        // 默认供应商
        this.autoFillOrderWithDefaultSupplier = false;
        // 支持临时供应商
        this.supportTemporarySupplier = false;
        // 支持临时客户
        this.supportTemporaryCustomer = false;


        this._enableMultiWarehouse = false;
        this.enableCommodityInventoryAdjustSimpleMode = true;

        this._supportDefaultCustomer = true;
        this.autoFillOrderWithDefaultCustomer = true;

        this._precision = Definition.defaultPrecisionLen;
        this._fundPrecision = Definition.gDefaultFundPrecision;

        this._enableMultiStarndedCommodity = false;
        this._enableNegtiveInventory = true;
        this.showPendingDealingFundPositiveReturnNum = true;

        this._taxRate = new CsDouble(taxRate, { precision: -1 });
        this.salesStaticWithTax = true;
        this._enablePurchaseWithTaxDetail = false;
        this._enableSalesWithTaxDetail = false;

        this.printOrderPretitle = "";
        this.printOrderContractNum = "";

        this._multiStoreMode = false;
        this._enableIntegerUnit = false;
        this._forceToShowOrderStoreInfo = false;

        this._enableCommodityNumDecimalPrecisionDown = true;
        this._enableFundDecimalPrecisionDown = true;
        this._enableSalesRelatedOrdersQuickMode = true;

        this._enableSalesFullPayment = true;
        this._salesOrderInventoryHandleMethod = OrderInventoryHandleMethod.QuickInventory;

        this._enableQuickDealingFundType = true;
        this._targetObjTotalAccountAmountMode = true;

        this._enableShowTargetObjFundAccountRecords = false;
        this._enableQuickQuotationMode = true;
        this._enablePurchaseRelatedOrdersQuickMode = true;
        this._enablePurchaseFullPayment = true;
        this._purchaseOrderInventoryHandleMethod = OrderInventoryHandleMethod.QuickInventory;
        this._enableInventoryRelatedOrdersQuickMode = true;
    }

    get precision() {
        return this._precision;
    }

    set precision(value) {
        if (value < 0) {
            this._precision = 0;
        } else if (value > Definition.maxPrecisionLen) {
            this._precision = Definition.maxPrecisionLen;
        } else {
            this._precision = value;
        }
    }

    get fundPrecision() {
        return this._fundPrecision;
    }

    set fundPrecision(value) {
        if (value < 0) {
            this._fundPrecision = 0;
        } else if (value > Definition.gMaxFundPrecisionLen) {
            this._fundPrecision = Definition.gMaxFundPrecisionLen;
        } else {
            this._fundPrecision = value;
        }
    }

    get enableMultiWarehouse() {
        return this._enableMultiWarehouse;
    }

    set enableMultiWarehouse(value) {
        this._enableMultiWarehouse = value;
    }

    get supportDefaultCustomer() {
        return this._supportDefaultCustomer;
    }

    set supportDefaultCustomer(value) {
        this._supportDefaultCustomer = value;
        if (!value) {
            this.autoFillOrderWithDefaultCustomer = false;
        }
    }

    get taxRate() {
        return this._taxRate.value;
    }

    setTaxRate(value) {
        this._taxRate = new CsDouble(value, { precision: Definition.gConstTaxRatePrecision });
    }

    get enableMultiStarndedCommodity() {
        return this._enableMultiStarndedCommodity;
    }

    set enableMultiStarndedCommodity(value) {
        this._enableMultiStarndedCommodity = value;
    }

    get enableNegtiveInventory() {
        return this._enableNegtiveInventory;
    }

    set enableNegtiveInventory(value) {
        this._enableNegtiveInventory = value;
    }

    get enablePurchaseWithTaxDetail() {
        return this._enablePurchaseWithTaxDetail;
    }

    set enablePurchaseWithTaxDetail(value) {
        this._enablePurchaseWithTaxDetail = value;
    }

    get enableSalesWithTaxDetail() {
        return this._enableSalesWithTaxDetail;
    }

    set enableSalesWithTaxDetail(value) {
        this._enableSalesWithTaxDetail = value;
    }

    get multiStoreMode() {
        return this._multiStoreMode;
    }

    set multiStoreMode(value) {
        this._multiStoreMode = value;
    }

    get enableIntegerUnit() {
        return this._enableIntegerUnit;
    }

    set enableIntegerUnit(value) {
        this._enableIntegerUnit = value;
    }

    get forceToShowOrderStoreInfo() {
        return this._forceToShowOrderStoreInfo;
    }

    set forceToShowOrderStoreInfo(value) {
        this._forceToShowOrderStoreInfo = value;
    }

    get enableCommodityNumDecimalPrecisionDown() {
        return this._enableCommodityNumDecimalPrecisionDown;
    }

    get enableFundDecimalPrecisionDown() {
        return this._enableFundDecimalPrecisionDown;
    }

    get enableSalesRelatedOrdersQuickMode() {
        return this._enableSalesRelatedOrdersQuickMode;
    }

    get enableSalesFullPayment() {
        return this._enableSalesFullPayment;
    }

    set enableSalesFullPayment(value) {
        this._enableSalesFullPayment = value;
    }

    get salesOrderInventoryHandleMethod() {
        return this._salesOrderInventoryHandleMethod;
    }

    set salesOrderInventoryHandleMethod(value) {
        this._salesOrderInventoryHandleMethod = value;
    }

    get enableQuickFundType() {
        return this._enableQuickDealingFundType;
    }

    get targetObjTotalAccountAmountMode() {
        return this._targetObjTotalAccountAmountMode;
    }

    get enableShowTargetObjFundAccountRecords() {
        return this._enableShowTargetObjFundAccountRecords;
    }

    set enableShowTargetObjFundAccountRecords(value) {
        this._enableShowTargetObjFundAccountRecords = value;
    }

    get enableQuickQuotationMode() {
        return this._enableQuickQuotationMode;
    }

    get enablePurchaseRelatedOrdersQuickMode() {
        return this._enablePurchaseRelatedOrdersQuickMode;
    }

    get enablePurchaseFullPayment() {
        return this._enablePurchaseFullPayment;
    }

    set enablePurchaseFullPayment(value) {
        this._enablePurchaseFullPayment = value;
    }

    get purchaseOrderInventoryHandleMethod() {
        return this._purchaseOrderInventoryHandleMethod;
    }

    set purchaseOrderInventoryHandleMethod(value) {
        this._purchaseOrderInventoryHandleMethod = value;
    }

    get enableInventoryRelatedOrdersQuickMode() {
        return this._enableInventoryRelatedOrdersQuickMode;
    }

    static fromJson(json) {
        const settings = new OrgSettings();
        settings._taxRate = Util.jsonToCsDouble(json.defaultTaxRatio, 13, { precision: Definition.gConstTaxRatePrecision });
        settings._precision = Util.jsonToInt(json.precision, Definition.defaultPrecisionLen);
        settings._enableMultiStarndedCommodity = Util.jsonToBool(json.multiStandardCommodityOn, false);
        settings._enableNegtiveInventory = Util.jsonToBool(json.negtiveInventoryOn, true);
        settings.printOrderContractNum = Util.jsonToString(json.printOrderContractNum);
        settings.printOrderPretitle = Util.jsonToString(json.printOrderPretitle);
        settings.salesStaticWithTax = Util.jsonToBool(json.staticStaticWithTax, true);
        settings._multiStoreMode = Util.jsonToBool(json.multiStoreMode, false);
        settings._fundPrecision = Util.jsonToInt(json.fundPrecision, Definition.gDefaultFundPrecision);
        settings._enableSalesFullPayment = Util.jsonToBool(json.enableSalesFullPayment, true);
        settings._salesOrderInventoryHandleMethod = OrderInventoryHandleMethod.fromCode(json.salesOrderInventoryHandleMethod);
        settings._enablePurchaseFullPayment = Util.jsonToBool(json.enablePurchaseFullPayment, true);
        settings._purchaseOrderInventoryHandleMethod = OrderInventoryHandleMethod.fromCode(json.purchaseOrderInventoryHandleMethod);
        settings._enableSalesWithTaxDetail = Util.jsonToBool(json.salesCommodityWithTax, false);
        settings._enablePurchaseWithTaxDetail = Util.jsonToBool(json.purchaseCommodityWithTax, false);
        settings._enableMultiWarehouse = Util.jsonToBool(json.enableMultiStorage, false);

        settings.precision = settings._precision;
        settings.fundPrecision = settings._fundPrecision;

        return settings;
    }

    toJson() {
        const json = {};
        json.defaultTaxRatio = this._taxRate.value;
        json.precision = this._precision;
        json.multiStandardCommodityOn = this._enableMultiStarndedCommodity ? 1 : 0;
        json.updateUser = Engine.profile?.nickName ?? '';
        json.negtiveInventoryOn = this._enableNegtiveInventory ? 1 : 0;
        json.printOrderContractNum = this.printOrderContractNum;
        json.printOrderPretitle = this.printOrderPretitle;
        json.staticStaticWithTax = this.salesStaticWithTax ? 1 : 0;

        // if (Engine.profile?.permissionMgr?.hasPermission?.([FunctionPermissionDefinition.P_Store_Edit])) {
        //   json.multiStoreMode = this._multiStoreMode ? 1 : 0;
        // }

        json.fundPrecision = this._fundPrecision;
        json.enableSalesFullPayment = this._enableSalesFullPayment ? 1 : 0;
        json.salesOrderInventoryHandleMethod = this._salesOrderInventoryHandleMethod.code;
        json.enablePurchaseFullPayment = this._enablePurchaseFullPayment ? 1 : 0;
        json.purchaseOrderInventoryHandleMethod = this._purchaseOrderInventoryHandleMethod.code;
        json.salesCommodityWithTax = this._enableSalesWithTaxDetail ? 1 : 0;
        json.purchaseCommodityWithTax = this._enablePurchaseWithTaxDetail ? 1 : 0;
        json.enableMultiStorage = this._enableMultiWarehouse ? 1 : 0;

        return json;
    }
}
